#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/12/11
# @Author :songtx
# @File   :write_mysql.py

import pymysql


def write_sql(time, o_i, status):  # 写入本地数据库，o_i有三个选项，do_s=513,di_s,000=err_msg
    command1 = ''
    # open_connect_mysql
    db = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cursor = db.cursor()
    if o_i == 513 or o_i == 769:
        if o_i == 513:
            command1 = 'INSERT INTO do_s(time,do_01,do_02,do_03,do_04,do_05,do_06,do_07,do_08,' \
                       'do_09,do_10,do_11,do_12,do_13,do_14,do_15,do_16,' \
                       'do_17,do_18,do_19,do_20,do_21,do_22,do_23,do_23,' \
                       'do_25,do_26,do_27,do_28,do_29,do_30,do_31,do_32)'
        elif o_i == 769:
            command1 = 'INSERT INTO di_s(time,di_01,di_02,di_03,di_04,di_05,di_06,di_07,di_08,' \
                       'di_09,di_10,di_11,di_12,di_13,di_14,di_15,di_16,' \
                       'di_17,di_18,di_19,di_20,di_21,di_22,di_23,di_24,' \
                       'di_25,di_26,di_27,di_28,di_29,di_30,di_31,di_32)'
        command2 = command1 + ' VALUE(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,' \
                              '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
        cursor.executemany(command2, [(time, status[0:1], status[1:2], status[2:3], status[3:4],
                                       status[4:5], status[5:6], status[6:7], status[7:8],
                                       status[8:9], status[9:10], status[10:11], status[11:12],
                                       status[12:13], status[13:14], status[14:15], status[15:16],
                                       status[16:17], status[17:18], status[18:19], status[19:20],
                                       status[20:21], status[21:22], status[22:23], status[23:24],
                                       status[24:25], status[25:26], status[26:27], status[27:28],
                                       status[28:29], status[29:30], status[30:31], status[31:32])])
    # if o_i == 000:
    #     command1 = 'INSERT INTO err_msg(time,err_01,err_02)'
    #     cursor.executemany(command1，)  # 未完成+++++++++++++++++++++++++++++++++++++
    db.commit()
    db.close()
